// pages/mt_fillPit_index/mt_fillPit_index.js
var app = getApp()
Page({

  data: {
    roomListFull: [],   //房间临时总容器，用于倒换数据
    roomList: [],       //房间当前展示容器
    pwdArea: true,
    mainArea:false
  },



  onLoad: function (options) {
   // app._fillPit_truncateRoom(app)
  },


  onShow:function(){
    wx.showLoading({
      title: '处理中......'
    })
    var t = this
    t._getRoomList(function(){
      wx.hideLoading()
    })
  },



  //获取房间列表
  _getRoomList(fun) {
    var v = 1;
    var t = this
    if (v)app.C('roomList from wx');
    if(wx.getStorageSync("roomList")){
      if (!wx.getStorageSync("roomList_expire") || wx.getStorageSync("roomList_expire")<new Date().getTime()){
        wx.setStorageSync("roomList",'');
        t._getRoomList()
      }
      t.setData({
        roomList: wx.getStorageSync("roomList")
      })
      if (fun) { fun() }
    }else{
      if(v)app.C('roomList from after');
      app._callYunFun({}, 'fillPit_getRoomList', function (res) {
        wx.setStorageSync("roomList", res.result.data)
        t.setData({
          roomList: wx.getStorageSync("roomList")
        })
        wx.setStorageSync("roomList_expire", new Date().getTime()+360000*24)
        if (fun) { fun() }
      });
    }
  },






  //搜索房间按键提交
  _getFormData(e) {
    wx.showLoading({
      title: '数据加载中...',
      mask: true
    })
    var t = this
    var search = e.detail.value.searchName

    var roomList = wx.getStorageSync("roomList");   //获取原全部数据

    if (!search) {  //无值
      t.setData({
        roomList:roomList
      })
      wx.hideLoading()
    }else{          //有值

      //先本地找
      var tmpList = []
      for(var i=0;i<roomList.length;i++){
        if(roomList[i].roomName.indexOf(search) >= 0){
          tmpList.push(roomList[i])
        }
      }
      if(tmpList.length > 0){
        t.setData({
          roomList:tmpList
        })
        wx.hideLoading()
      }else{  //本地没有，向后台请求
        var db = wx.cloud.database({
          env: 'tpcwre1'
        })
        var tb = db.collection("microTools_fillPit")
        tb.where({
          roomName: search
        }).field({
          _id:true,
          roomName:true,
          roomPwd:true
        }).get({
          success: function (res) {
            if (res.data.length > 0) {    //有数据就加载到缓存和临时变量中
              var tmpStore = wx.getStorageSync("roomList")
              tmpStore.unshift(res.data[0]) //添加到缓存
              wx.setStorageSync("roomList", tmpStore)
              tmpList.unshift(res.data[0]);
            }
            t.setData({
              roomList: tmpList
            })
            wx.hideLoading()
          }
        })
      }


      
    }
  },




  //跳转到房间
  _viewPwdArea(e) {
    var index = e.currentTarget.dataset.index;
    app.globalData.fillPit_currentRoom = this.data.roomList[index];

    wx.navigateTo({
      url: '/pages/mt_fillPit_room/mt_fillPit_room',
    })
  },

 




  //跳转到创建房间
  _createRoom(){
    wx.navigateTo({
      url: '/pages/mt_fillPit_roomCreate/mt_fillPit_roomCreate',
    })
  },



  onShareAppMessage: function () {

  }
})